/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Control;

import Model.Connect;
import Model.Dep;
import Model.Medecin;
import Model.Pays;
import Model.Spe;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Collection;
import java.util.TreeSet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author sio
 */
public class Control extends HttpServlet {

    private Pays p;
    private Collection med;
    private Collection medi;

    public void init(ServletConfig config) throws ServletException {
        super.init(config);
        p = new Pays();
    }

    public void destroy() {
        Connect.close();
    }

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        String page = null;
        if (null != request.getParameter("choix")) {
            if ("nom".equals(request.getParameter("choix"))) {
                String noms = request.getParameter("nom");
                if (null == noms) {
                    page = "nomMedecin.jsp";
                } else {
                    Collection<Medecin> nomsMed = new TreeSet<Medecin>();
                    Collection<Dep> lesDeps = p.getLesDeps();
                    for (Dep dep : lesDeps) {
                        Collection<Medecin> lesMeds = dep.getLesMeds();
                        for (Medecin med : lesMeds) {
                            if (med.getNom().startsWith(noms)) {
                                nomsMed.add(med);
                            }
                        }
                        request.setAttribute("nom", noms);
                        request.setAttribute("meds", nomsMed);
                    }
                    page = "nomMed.jsp";
                }
            } else if("departement".equals(request.getParameter("choix"))) {
                String dep = request.getParameter("dep");
                if (null == dep) {
                    Collection<Dep> lesDeps = p.getLesDeps();
                    request.setAttribute("deps", lesDeps);
                    page = "departementMedecin.jsp";
                } else {
                    Collection<Medecin> lesMeds = p.getLeDep(dep).getLesMeds();
                    request.setAttribute("dep", dep);
                    request.setAttribute("meds", lesMeds);
                    page = "depMedecin.jsp";
                }
            }
            else {
                String spe = request.getParameter("spe");
                if (null == spe) {
                    Collection<Spe> lesSpe = p.getLesSpe();
                    request.setAttribute("spes", lesSpe);
                    page = "specialisationMedecin.jsp";
                } else {
                    Collection<Medecin> lesMeds = p.getLaSpe(spe).getLesMeds();
                    request.setAttribute("spe", spe);
                    request.setAttribute("meds", lesMeds);
                    page = "speMedecin.jsp";
                }
            }
        } else {
            page = "index.jsp";
        }
        request.getRequestDispatcher(page).forward(request, response);
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
